package com.example.demo.service.impl;



import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.PageBean;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserImpl implements UserService {
    @Autowired
    private UserMapper userMapper;//自动创建对象,连接起来

    @Override
    public List<User> selectUser() {

        List<User> UserList = userMapper.selectUser();

        return UserList;
    }

    @Override
    public void save(User user) {
        userMapper.save(user);
    }

    @Override
    public List<User> selectUserCm() {
        List<User> UserList = userMapper.selectUserCm();
        return UserList;
    }

    @Override
    public void saveCm(User user) {
        userMapper.saveCm(user);
    }

    @Override
    public List<User> selectUserAd() {
        List<User> UserList = userMapper.selectUserAd();
        return UserList;
    }

    @Override
    public void saveAd(User user) {
        userMapper.saveAd(user);
    }

    @Override
    public void deleteStu(String account) {
        userMapper.deleteStu(account);
    }

    @Override
    public void deleteCompany(String account) {
        userMapper.deleteCompany(account);
    }

    @Override
    public PageBean<User> list(Integer pageNum, Integer pageSize) {
        //1.创建PageBean对象
        PageBean<User> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum, pageSize);

        //3.调用mapper
//            Map<String,Object> map = ThreadLocalUtil.get();
//            Integer userId = (Integer) map.get("id");
//            List<Article> as = articleMapper.list(userId,categoryId,state);
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        List<User> as = userMapper.selectUser();
        Page<User> p = (Page<User>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;

    }


}
